package com.wen.edu.controller;


import com.wen.commonutils.ResultEntity;
import com.wen.edu.entity.EduChapter;
import com.wen.edu.entity.vo.ChapterVO;
import com.wen.edu.service.EduChapterService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author wen
 * @since 2021-05-29
 */
@RestController
@RequestMapping("/edu/service/chapter")
public class EduChapterController {
    @Resource
    private EduChapterService eduChapterService;

    /**
     * 根据相关的课程ID来获取对应的课程大纲（课程章节和小节）
     *
     * @param courseId 课程ID
     * @return result of operation
     */
    @GetMapping("getChapterVideo/{courseId}")
    public ResultEntity getChapterVideo(@PathVariable String courseId) {
        List<ChapterVO> list = eduChapterService.getChapterVideoByCourseId(courseId);
        return ResultEntity.ok().data("chapterVO", list);
    }

    /**
     * 添加课程章节
     *
     * @param eduChapter 课程章节
     * @return result of operation
     */
    @PostMapping("addChapter")
    public ResultEntity addChapter(@RequestBody EduChapter eduChapter) {
        eduChapterService.save(eduChapter);
        return ResultEntity.ok();
    }

    /**
     * 根据章节id查询信息
     *
     * @param chapterId 章节ID
     * @return result of operation
     */
    @GetMapping("getChapter/{chapterId}")
    public ResultEntity getChapterInfo(@PathVariable("chapterId") String chapterId) {
        EduChapter eduChapter = eduChapterService.getById(chapterId);
        return ResultEntity.ok().data("eduChapter", eduChapter);
    }

    /**
     * 修改课程章节
     *
     * @param eduChapter 课程章节
     * @return result of operation
     */
    @PostMapping("updateChapter")
    public ResultEntity updateChapter(@RequestBody EduChapter eduChapter) {
        eduChapterService.updateById(eduChapter);
        return ResultEntity.ok();
    }

    /**
     * 根据章节id删除章节信息(需要删除章节下面的存储在阿里云上的视频信息)
     *
     * @param chapterId 章节ID
     * @return result of operation
     */
    @DeleteMapping("deleteChapter/{chapterId}")
    public ResultEntity deleteChapterInfo(@PathVariable("chapterId") String chapterId) {
//        删除章节
        eduChapterService.deleteChapter(chapterId);
        return ResultEntity.ok();
    }
}

